package com.ruoyi.weiriya.service.impl;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
 
import java.io.IOException;

public class HttpUtil {

    public static String sendPostReq(String url, String param,String encoding,int contentType, String accessKey, String cookie) throws IOException {
        String body = "";

        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost (url);

        //设置请求参数实体
        StringEntity reqParam = new StringEntity (param, "utf-8");
        reqParam.setContentEncoding(new BasicHeader (HTTP.CONTENT_TYPE,
                "application/json"));
        //将请求参数放到请求对象中
        httpPost.setEntity(reqParam);

        //设置请求报文头信息
        httpPost.setHeader ("Accept", "application/json, text/javascript, */*; q=0.01");
        if (contentType == 0){
            httpPost.setHeader("Content-type", "application/json;charset=UTF-8");//设置发送JSON请求
        }else if (contentType == 1){
            httpPost.setHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");//设置发送表单请求
        }

        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 9; CLT-AL01 Build/HUAWEICLT-AL01; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36 Html5Plus/1.0");
        if(StringUtils.isNotBlank(accessKey)) {
            httpPost.setHeader("access_key", accessKey);
        }
        if(StringUtils.isNotBlank(cookie)) {
            httpPost.setHeader("Cookie", cookie);
        }
        //发送请求，并拿到结果（同步阻塞）
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //获取返回结果
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, encoding);
        }
        EntityUtils.consume(entity);
        //释放Http请求链接
        response.close();
        return body;
    }

    public static String sendPostReqGetCookie(String url, String param,String encoding,int contentType, String accessKey) throws IOException {

        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost (url);

        //设置请求参数实体
        StringEntity reqParam = new StringEntity (param, "utf-8");
        reqParam.setContentEncoding(new BasicHeader (HTTP.CONTENT_TYPE,
                "application/json"));
        //将请求参数放到请求对象中
        httpPost.setEntity(reqParam);

        //设置请求报文头信息
        httpPost.setHeader ("Accept", "application/json, text/javascript, */*; q=0.01");
        if (contentType == 0){
            httpPost.setHeader("Content-type", "application/json;charset=UTF-8");//设置发送JSON请求
        }else if (contentType == 1){
            httpPost.setHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");//设置发送表单请求
        }

        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 9; CLT-AL01 Build/HUAWEICLT-AL01; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36 Html5Plus/1.0");
        if(StringUtils.isNotBlank(accessKey)) {
            httpPost.setHeader("access_key", accessKey);
        }
        //发送请求，并拿到结果（同步阻塞）
        CloseableHttpResponse response = httpClient.execute(httpPost);
        Header[] allHeaders = response.getAllHeaders();
        String Cookie = response.getHeaders("Set-Cookie")[0].getValue().split(";")[0];
        //获取返回结果
        HttpEntity entity = response.getEntity();

        EntityUtils.consume(entity);
        //释放Http请求链接
        response.close();
        return Cookie;
    }

}